<?php
namespace app\app\controller;

use app\common\controller\App;
use think\Db;
use think\Exception;


class Collect extends App
{
    protected $middleware = ['AppAuth'];

    public function getList()
    {
        $uid=$this->getLoginUid();
        $page=request()->param("page",'1','intval');
        $list=model("Collect")
            ->alias("a")
            ->leftJoin("House b","a.house=b.id")
            ->where(['a.uid'=>$uid])
            ->order('a.id desc')
            ->page($page,8)
            ->field("a.id,a.house,b.price,b.tab,b.area_big,b.images,b.type,b.title")
            ->select();
        $shuxing=model("House")::TYPE;
        if (!empty($list)){
            foreach ($list as $key=>$val){
                $list[$key]['image_path']=model("File")->getPath($val['images']);
                $spec=isset($shuxing[$val['type']])?$shuxing[$val['type']]:'';
                $specs=$val['area_big'].'㎡|'.$spec;
                $list[$key]['spec_title']=$specs;
                $list[$key]['sign']=model("Sign")->get_house_spec($val['tab']);
            }
        }
                return $this->ok('请求成功',$list);


    }

    public function del()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $res=model("Collect")
            ->where(['uid'=>$uid,'id'=>$id])
            ->delete();
        if ($res){
            return $this->ok('删除成功');
        }else{
            return $this->fail('删除失败');
        }
    }

    //收藏
    public function add_collect()
    {
        $id=request()->param("id",'0','intval');
       if (empty($id)){
           return $this->fail('参数有误');
       }
        $uid=$this->getLoginUid();
        $has_collect=model("Collect")->where(['uid'=>$uid,'house'=>$id])->find();
        if (empty($has_collect)){

            $res=model("Collect")->insert(['house'=>$id,'uid'=>$uid,'create_time'=>time()]);
            if ($res){
                return $this->ok('收藏成功',['is_collect'=>1]);
            }else{
                return $this->fail('收藏失败');
            }
        }else{
            $res=model("Collect")->where(['id'=>$has_collect['id']])->delete();
            if ($res){
                return $this->ok('取消收藏成功',['is_collect'=>0]);
            }else{
                return $this->fail('取消收藏失败');
            }
        }
    }

}
